package com.water.wfo.user.business.service.impl;

import com.water.wfo.component.api.exception.BusinessException;
import com.water.wfo.sys.pojo.entity.RoleEntity;
import com.water.wfo.sys.pojo.entity.UserEntity;
import com.water.wfo.sys.pojo.entity.UserExtEntity;
import com.water.wfo.sys.pojo.request.UserQueryRequest;
import com.water.wfo.sys.pojo.request.UserRequest;
import com.water.wfo.sys.pojo.response.UserResponse;
import com.water.wfo.user.ability.UserAbility;
import com.water.wfo.user.ability.UserExtAbility;
import com.water.wfo.user.business.service.IUserService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * packageName com.water.wfo.user.business.service.impl
 *
 * @author
 * @className UserServiceImpl(处以class为网)
 * @date 2024/8/27
 * @description T0D0
 */
@Component
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserAbility userAbility;
    @Autowired
    private UserExtAbility userExtAbility;

    @Override
    public UserResponse queryUserListInfo(UserQueryRequest request) {
        return userAbility.queryUserListInfo(request);
    }

    @Override
    @Transactional
    public void saveUser(UserRequest userEntity) throws BusinessException{
        UserEntity newUser = new UserEntity();
        BeanUtils.copyProperties(userEntity,newUser);
        UserEntity queryUser = userAbility.selectUserById(newUser.getLoginId());
        if(null != queryUser){
            throw  new BusinessException("账号已存在");
        }
        userAbility.insert(newUser);
        UserEntity entity = userAbility.selectUserById(newUser.getLoginId());
        if(null != entity){
            UserExtEntity extEntity = new UserExtEntity();
            extEntity.setUserId(entity.getId());
            extEntity.setLoginId(entity.getLoginId());
            extEntity.setRoleId(userEntity.getRoleId());
            userExtAbility.insert(extEntity);
        }
    }

    @Override
    public void updateUser(UserRequest userEntity) throws BusinessException {
        UserEntity newUser = new UserEntity();
        BeanUtils.copyProperties(userEntity,newUser);
        userAbility.update(newUser);
        UserExtEntity paramExt = new UserExtEntity();
        paramExt.setUserId(newUser.getId());
        List<UserExtEntity> userExtEntities = userExtAbility.queryUserExtEntityBy(paramExt);
        if(CollectionUtils.isNotEmpty(userExtEntities)){
            UserExtEntity extEntity = userExtEntities.get(0);
            extEntity.setRoleId(userEntity.getRoleId());
            userExtAbility.update(extEntity);
        }else {
            UserExtEntity extEntity = new UserExtEntity();
            extEntity.setUserId(newUser.getId());
            extEntity.setLoginId(newUser.getLoginId());
            extEntity.setRoleId(userEntity.getRoleId());
            userExtAbility.insert(extEntity);
        }
    }
}
